﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using MT.eVotePRS.Model;
using MT.eVotePRS.Presentation;
using MT.eVotePRS.Presentation.Presenter;

namespace MT.eVotePRS.UI.UserControls
{
    public partial class CheckIn : UserControl, IViewCheckIn
    {
        #region IViewCheckIn
        
        Handsets _handset;
        StudentInfo _student;
        InventoryTxn _txn;

        public Handsets Handset
        {
            get
            {
                _handset.MAC = txtHandsetID.Text;
                return _handset;
            }
            set
            {
                _handset = value;
                txtHandsetID.Text = _handset.MAC;
            }
        }

        public StudentInfo Student
        {
            get { return _student; }
            set
            {
                _student = value;
                txtStudentID.Text = _student.StudentNo;
                txtStudentName.Text = _student.StudentName;
            }
        }

        public InventoryTxn Txn
        {
            get { return _txn; }
            set
            {
                _txn = value;
                txtStatus.Text = _txn.UserEvent;
                txtDatetime.Text = _txn.TxnTime.ToShortDateString();
                txtRemark.Text = _txn.Remark;
            }
        }

        public string EventRemark
        {
            get { return txtRemark.Text; }
        }

        public event EventHandler HandsetEntered;
        public event EventHandler CheckInClicked;

        public void SetError(string propertyName, string errorMessage)
        {
        }

        public void EnableCheckIn()
        {
            btnConfirm.Enabled = true;
        }

        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        // Event firing helpers
        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handlers = PropertyChanged;
            if (handlers != null)
            {
                handlers(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public CheckIn()
        {
            InitializeComponent();
            txtHandsetID.KeyDown += OnKeyDown;
            this.btnConfirm.Click += OnCheckInClicked;
        }

        void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                EventHandler handler = HandsetEntered;
                if (handler != null)
                {
                    handler(this, EventArgs.Empty);
                }
            }
        }

        void OnCheckInClicked(object sender, EventArgs e)
        {
            EventHandler handlers = CheckInClicked;

            if (handlers != null)
            {
                handlers(this, EventArgs.Empty);
            }
        }
    }
}
